"""
首页例子的UI层。
"""

import typing
from elio import core
from .activity import ActivityFuncLayer
from .dialog import DialogLayer
from .gesture import GestureLayer
from .listBox import ListBoxLayer
from .track import TrackLayer
from .sound import SoundPoolLayer
from .timeConsuming import TimeConsumingTaskLayer
from .tts import TtsLayer
from .widget import WidgetLayer
from .fileUploader import FileUploaderLayer
from .player import PlayerLayer
from .recorder import RecorderLayer
from .requests import RequestsLayer
from .sensor import SensorLayer
from .file_rw import FileLayer


class HomeLayer(core.Layer[core.Activity]):
    layer: core.Layer | None = None
    buttons: list[core.WidgetButton] = []
    layer_dict: dict[str, core.Layer]

    async def on_create(self, *args: typing.Any, **kwargs: typing.Dict):
        await super().on_create(*args, *kwargs)
        activity = self.parent

        # 在此处添加测试项目
        self.layer_dict = {
            '测试关于activity的函数': await ActivityFuncLayer.create(activity),
            '测试播放器': await PlayerLayer.create(activity),
            '测试语音库': await TtsLayer.create(activity),
            '测试音频轨道': await TrackLayer.create(activity),
            '测试小部件': await WidgetLayer.create(activity),
            '测试对话框': await DialogLayer.create(activity),
            '测试手势事件监听': await GestureLayer.create(activity),
            '测试列表框': await ListBoxLayer.create(activity),
            '测试声音池': await SoundPoolLayer.create(activity),
            '测试耗时任务': await TimeConsumingTaskLayer.create(activity),
            '测试录音功能': await RecorderLayer.create(activity),
            '测试网络资源获取功能': await RequestsLayer.create(activity),
            '测试文件上传器': await FileUploaderLayer.create(activity),
            '测试传感器': await SensorLayer.create(activity),
            '测试文件读写': await FileLayer.create(activity),

        }

    async def on_show(self):
        await super().on_show()
        await self.parent.get_screen_size()  # 添加这行解决屏幕压缩显示
        await self.init_ui()

    async def on_hide(self):
        await super().on_hide()
        await self.un_init_ui()

    async def init_ui(self):
        self.buttons.clear()
        i = 0
        for name, layer in self.layer_dict.items():
            self.buttons.append(await self.create_button(name, 50 * i, self.cb_btn(layer)))
            i += 1

    async def un_init_ui(self):
        for btn in self.buttons:
            if btn is not None:
                await btn.destroy()

    async def on_pause(self):
        await super().on_pause()

    async def on_resume(self):
        await super().on_resume()

    async def on_destroy(self, *args: typing.Any, **kwargs: typing.Dict):
        await super().on_destroy(*args, **kwargs)
        for layer in self.layer_dict.values():
            if layer is not None:
                await layer.destroy()

    async def create_button(self, name: str, pos_y: int, cb: typing.Callable) -> core.WidgetButton:
        """
        创建按钮。
        :param name: 按钮文字。
        :param pos_y: 纵坐标。
        :param cb: 点击处理函数。
        :return: core.WidgetButton
        """
        activity = self.parent
        width, _ = await activity.get_screen_size()

        button = await core.WidgetButton.create(activity, width - 60, 50, 30, pos_y, name)
        await button.set_on_click_listener(cb)

        return button

    def cb_btn(self, layer: typing.Any):
        async def cb():
            nonlocal layer
            self.layer = layer
            await self.parent.push_layer(self.layer)

        return cb
